﻿Imports DAL
Imports System.Linq
Imports System.Threading
Public Class FrmConnexion
    Private formEnMouvement As Boolean = False
    Private PosCurseur As Point
    Private renderer As VisualStyles.VisualStyleRenderer
    Private windowElements As Dictionary(Of String, VisualStyles.VisualStyleElement) = New Dictionary(Of String, VisualStyles.VisualStyleElement)
    Private render As Boolean = False


    Private Sub FrmConnexion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim bd2 As New BDCarpeDiemDataContext
        PbLogo.Parent = PbBackground
        BtnFermer.Parent = PbBackground
        BtnReduire.Parent = PbBackground
        TableLayoutPanel1.Parent = PbBackground
        If Not String.IsNullOrEmpty(My.Settings.Login) Then
            tbUserName.Text = My.Settings.Login
            CkbSouvenir.Checked = True
        End If

        AnimationOuverture()
        Dim employes = From i In bd2.Employes Select i
        Dim parametres = From i In bd2.Parametres Select i
        Dim parametre As New Parametre
        Dim employe As Employe
        If (From emp In bd.Employes Where emp.Supprime = False Select emp).Count = 0 Then
            employe = New Employe
            employe.GestionBeneficiaire = True
            employe.GestionEmploye = True
            employe.GestionHoraire = True
            employe.Login = "admin"
            employe.Nom = "Admin"
            employe.Prenom = "Admin"
            employe.Note = "Employé créé automatiquement"
            employe.Password = SimpleCrypt("admin")
            employe.Poste = 3
            bd2.Employes.InsertOnSubmit(employe)
        Else
            If employes.Count = 1 Then
                employe = employes.First
                employe.GestionBeneficiaire = True
                employe.GestionEmploye = True
                employe.GestionHoraire = True
            End If
        End If
        If parametres.Count = 0 Then
            parametre.CouleurAccompagnement = "#33ffff"
            bd2.Parametres.InsertOnSubmit(parametre)
        End If
        bd2.SubmitChanges()
    End Sub

    'DEBUT DE L'ANIMATION D'OUVERTURE ******************************************************************

    Private Sub AnimationOuverture()
        Me.Width = 210
        Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
        Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2

        TableLayoutPanel1.Visible = False

        Dim iCount As Integer
        For iCount = 0 To 100 Step 5
            Me.Opacity = iCount / 100
            Me.Refresh()
            Threading.Thread.Sleep(50)
        Next

        Threading.Thread.Sleep(200)

        AnimationConnexion()
    End Sub

    Private Sub AnimationConnexion()
        Pbar_Loading.Visible = False
        Me.Top = (Screen.PrimaryScreen.WorkingArea.Height - Me.Height) / 2

        For i = 0 To 235 Step 10
            Me.Width = 214 + i
            Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
            Me.Refresh()
            Threading.Thread.Sleep(12 - ((10 * i) / 276))
        Next
        Me.Width = 445
        Me.Left = (Screen.PrimaryScreen.WorkingArea.Width - Me.Width) / 2
        Threading.Thread.Sleep(50)
        TableLayoutPanel1.Visible = True
        If CkbSouvenir.Checked Then
            tbPassword.Focus()
        End If
    End Sub

    Private Sub PbBackground_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PbBackground.Paint
        Dim g As Graphics = e.Graphics
        g.DrawLine(Pens.Gray, Me.Width - 2, 0, Me.Width - 2, Me.Height)
    End Sub

    'FIN DE L'ANIMATION D'OUVERTURE ********************************************************************

    'DEBUT DES FONCTIONS DE DEPLACEMENT DE LA FENETRE***************************************************

    Private Sub FrmConnexion_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown, PbLogo.MouseDown, TableLayoutPanel1.MouseDown, PbBackground.MouseDown
        If e.Button = MouseButtons.Left Then
            formEnMouvement = True
            PosCurseur = e.Location
        End If
    End Sub

    Private Sub FrmConnexion_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp, PbLogo.MouseUp, TableLayoutPanel1.MouseUp, PbBackground.MouseUp
        If e.Button = MouseButtons.Left Then
            formEnMouvement = False
        End If
    End Sub

    Private Sub FrmConnexion_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove, PbLogo.MouseMove, TableLayoutPanel1.MouseMove, PbBackground.MouseMove
        If formEnMouvement Then
            Dim temp As Point = New Point()
            temp.X = Me.Location.X + (e.X - PosCurseur.X)
            temp.Y = Me.Location.Y + (e.Y - PosCurseur.Y)
            Me.Location = temp
            temp = Nothing
        End If

    End Sub

    'FIN DES FONCTIONS DE DEPLACEMENT DE LA FENETRE*****************************************************

    'DEBUT GESTION DES BOUTONS DE CONTROLE DE LA FENETRE ***********************************************

    Private Sub BtnFermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFermer.Click
        Me.Close()
    End Sub

    Private Sub BtnReduire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReduire.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub BtnFermerEtBtnReduire_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReduire.MouseLeave, BtnFermer.MouseLeave
        Dim btn As Button = sender
        If btn.Name = "BtnFermer" Then
            BtnFermer.Image = My.Resources.BtnCloseNormal
        End If
        If btn.Name = "BtnReduire" Then
            BtnReduire.Image = My.Resources.BtnReduireNormal
        End If
    End Sub

    Private Sub BtnFermerEtBtnReduire_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles BtnReduire.MouseDown, BtnFermer.MouseDown
        Dim btn As Button = sender
        If btn.Name = "BtnFermer" Then
            BtnFermer.Image = My.Resources.BtnClosePressed
        End If
        If btn.Name = "BtnReduire" Then
            BtnReduire.Image = My.Resources.BtnReduirePressed
        End If
    End Sub

    Private Sub BtnFermerEtBtnReduire_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReduire.MouseEnter, BtnFermer.MouseEnter
        Dim btn As Button = sender
        If btn.Name = "BtnFermer" Then
            BtnFermer.Image = My.Resources.BtnCloseHot
        End If
        If btn.Name = "BtnReduire" Then
            BtnReduire.Image = My.Resources.BtnReduireHot
        End If
    End Sub

    'FIN GESTION DES BOUTONS DE CONTROLE DE LA FENETRE *************************************************

    'DEBUT DE L'AUTHENTIFICATION AU SERVEUR ************************************************************

    Private Sub BtnConnexion_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnexion.Click
        'VERIFIER LE NOM D'UTILISATEUR ET LE MOT DE PASSE
        Dim bd As New BDCarpeDiemDataContext
        If Not String.IsNullOrEmpty(tbUserName.Text) AndAlso Not String.IsNullOrEmpty(tbPassword.Text) Then
            Dim emp = From x In bd.Employes Where x.Login.ToUpper = tbUserName.Text.ToUpper AndAlso x.Password = SimpleCrypt(tbPassword.Text) AndAlso x.Supprime = False Select x

            If Not emp.Count = 0 Then
                If CkbSouvenir.Checked Then
                    My.Settings.Login = tbUserName.Text
                    My.Settings.Save()
                Else
                    If Not String.IsNullOrEmpty(My.Settings.Login) Then
                        My.Settings.Login = String.Empty
                        My.Settings.Save()
                    End If
                End If
                empConnecte.id = emp.First.ID
                empConnecte.gestionBeneficiaire = emp.First.GestionBeneficiaire
                empConnecte.gestionEmploye = emp.First.GestionEmploye
                empConnecte.gestionHoraire = emp.First.GestionHoraire
                empConnecte.nom = emp.First.Prenom & ", " & emp.First.Nom
                FrmAcceuil.TsbCurrentUser.Text = emp.First.Prenom & ", " & emp.First.Nom
                FrmAcceuil.Show()
                Me.Hide()
            Else
                MsgBox("Nom d'utilisateur ou mot de passe incorrect.", vbCritical)
            End If
        Else
            MsgBox("Un nom d'utilisateur et un mot de passe doivent être entrés", vbCritical)
        End If
    End Sub


    Private Sub TbLogin_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles tbUserName.KeyPress, tbPassword.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            If sender Is tbUserName Then
                tbPassword.Focus()
            End If
            If sender Is tbPassword Then
                BtnConnexion_Click(BtnConnexion, Nothing)
            End If
        End If
        
    End Sub
End Class